<?php

/*
 * Edit by shengxin
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\shop\model\RoomModel;
use app\shop\model\BookingModel;
/**
 * Description of Room
 *
 * @author PC
 */
class Room extends Api{
    
    protected function initialize() {
        parent::initialize();
        $this->dbToken();
    }
    
    public function roomlist(){
        $roomlist = [
            [
                'faceUrl'=>'/image',
                'roomName'=>'天字1号房间',
                'price'=>'88.00',
                'bed_num'=>3
            ]   , 
            [
                'faceUrl'=>'/image2',
                'roomName'=>'天字2号房间',
                'price'=>'188.00',
                'bed_num'=>3
            ]   
        ];
        $this->success('ok', $roomlist);
    }
    
    public function getRoomById(){
        $id = $this->request->get('id/d');
        $model = new RoomModel();
        $room = $model->where('id',$id)->field('id,bed_num,price,room_name')->find();
        $bookingModel = new BookingModel();
        
        $booking = $bookingModel->where('room_id', $id)->field('bed_id,date,start_time,end_time')->select();
        $data = $room;
        $data['booking']=$booking;
        $this->success('ok',$data);
    }
    
    /**
     * 预定床位，传入房间号，自动分配床位，
     * 房间已经存在的预定空位，判断是否重复，
     * 时间段和时长判断，是否满足要求
     * 插入数据
     */
    public function booking(){
        $uid = $this->dbToken();
        $params = $this->request->post();
        $roomid = $params['id'];
        $time_start = $params['start'];
        $time_end = $params['end'];
        $date = $params['date'];
        $validate = new \app\api\validate\BookingValidate();
        $result = $validate->check($params);
        if (true !== $result) {
            $this->error($validate->getError());
        }
        $len = $time_end-$time_start;
        if($len<60||$len>360){
            $this->error('不符合要求');
        }
        //数据符合要求
        
        
        $roomModel = new RoomModel();
        $room = $roomModel->where('id',$roomid)->find();
        $bednum = $room->bed_num;
        
        //数据是否冲突
        $bookModel = new BookingModel();
        $booklist = $bookModel->where('room_id',$roomid)->where('date',$date)->select()->toArray();
        $idx = 0;
        if(!empty($booklist)){
            foreach ($booklist as $booking){
                $start = $booking['start_time'];
                $end = $booking['end_time'];
                //起点，终点在区间内，或者完全重合
                if(($start<$time_start&&$time_start<$end)||($time_end>$start&&$end>$time_end)||($start==$time_start&&$end==$time_end)){
                    $idx++;
                }
            }
            if($idx>=$bednum){
                $this->error('没有可用的床位');
            }
        }
        //
        //插入数据
        $bed_id = $roomid*100+$idx;
        $data = ['bed_id'=>$bed_id,'room_id'=>$roomid,'user_id'=>$uid,'date'=>$date,'start_time'=>$time_start,'end_time'=>$time_end];
        $data['create_time']= time();
        $bookModel->insert($data);
        $this->success('ok',[]);
    }
    
    public function cancelBooking(){
        $uid = $this->dbToken();
        $id = $this->request->post('id');
        $bookModel = new BookingModel();
        $bookModel->where('id',$id)->where('user_id',$uid)->update(['enable_i'=>0]);
        $data = $bookModel->getUserActiveData($uid);
        $this->success('ok',['order'=>$data]);
    }
}
